﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;

namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for Deck_Container.xaml
	/// </summary>
	public partial class Deck_Container : UserControl
	{
		public Deck_Container()
		{
			this.InitializeComponent();
            this.container.Children.Clear();
            this.Loaded += new RoutedEventHandler(Deck_Container_Loaded);
		}

        void Deck_Container_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                
                layout = LayoutManager.LayoutManager.MainWindow.Content as DeckConstructLayout;
            }
            catch(Exception ex)
            {

            }
        }

        public void Add(YBaseCard card)
        {
            if (!Check(card)) return;

            Image img = new Image();
            img.Source = card.displayImage;
            img.Width = 60;
            img.Height = 80;
            if(container.Children.Count%15!=0)
            {
                img.Margin = new Thickness(-20, 5, 0, 0);
            }
            else
            {
                img.Margin = new Thickness(0, 5, 0, 0);
            }
            if (container.Children.Count>=15)
            {
                img.Margin = new Thickness(img.Margin.Left, img.Margin.Top-40, img.Margin.Right, img.Margin.Bottom);
            }
            img.DataContext = card;
            img.MouseEnter += new MouseEventHandler(img_MouseEnter);
            img.MouseLeave += new MouseEventHandler(img_MouseLeave);
            img.MouseDown += new MouseButtonEventHandler(img_MouseDown);
            this.container.Children.Add(img);
            cb_filter.SelectedIndex = -1;
            
        }
        public void Adjust()
        {
            int d = 0;
            foreach (Image img in container.Children)
            {
                if (d % 15 != 0)
                {
                    img.Margin = new Thickness(-20, 5, 0, 0);
                }
                else
                {
                    img.Margin = new Thickness(0, 5, 0, 0);
                }
                if (d >= 15)
                {
                    img.Margin = new Thickness(img.Margin.Left, img.Margin.Top - 40, img.Margin.Right, img.Margin.Bottom);
                }
                d++;
            }
        }
        public IEnumerable<int> GetCurrentDeck()
        {
            foreach (Image item in container.Children)
            {
                YBaseCard card = item.DataContext as YBaseCard;
                yield return card.Card_TypeID;
            }
        }

        void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Image img = ((Image)sender);
            img.MouseEnter -= new MouseEventHandler(img_MouseEnter);
            img.MouseLeave -= new MouseEventHandler(img_MouseLeave);
            img.MouseDown -= new MouseButtonEventHandler(img_MouseDown);
            this.container.Children.Remove(img);
            Adjust();
        }

        void img_MouseLeave(object sender, MouseEventArgs e)
        {
            Image img = ((Image)sender);
            img.Margin = new Thickness(img.Margin.Left, img.Margin.Top + 20, img.Margin.Right, img.Margin.Bottom);
        }

        void img_MouseEnter(object sender, MouseEventArgs e)
        {
            Image img=((Image)sender);
            YBaseCard card = ((Image)sender).DataContext as YBaseCard;
            layout.GetCardImageDetail().Source = card.displayImage;
            layout.GetCardTextDetail().SetText(card.Description);
            img.Margin = new Thickness(img.Margin.Left, img.Margin.Top - 20, img.Margin.Right, img.Margin.Bottom);
        }

        private void Filter_Change(object sender, SelectionChangedEventArgs e)
        {
            if (cb_filter.SelectedIndex == -1) return;

            switch( ((ComboBoxItem)cb_filter.SelectedItem).Content.ToString())
            {
                case "Type":
                    Sort_ByType();
                    Adjust();
                    break;
                case "Name":
                    Sort_ByName();
                    Adjust();
                    break;
            }
            
        }

        private void Sort_ByType()
        {
            UIElement temp1, temp2;
            YBaseCard c1, c2;
            for (int i = 0; i < container.Children.Count - 1; i++)
            {
                for (int j = i + 1; j < container.Children.Count; j++)
                {
                    c1 = ((Image)container.Children[i]).DataContext as YBaseCard;
                    c2 = ((Image)container.Children[j]).DataContext as YBaseCard;
                    if (c1.CardType > c2.CardType)
                    {
                        temp1 = container.Children[i];
                        temp2 = container.Children[j];
                        container.Children.RemoveAt(i);
                        container.Children.RemoveAt(j - 1);
                        container.Children.Insert(i, temp2);
                        container.Children.Insert(j, temp1);
                    }
                }
            }
        }

        private void Sort_ByName()
        {
            UIElement temp1, temp2;
            YBaseCard c1, c2;
            for (int i = 0; i < container.Children.Count - 1; i++)
            {
                for (int j = i + 1; j < container.Children.Count; j++)
                {
                    c1 = ((Image)container.Children[i]).DataContext as YBaseCard;
                    c2 = ((Image)container.Children[j]).DataContext as YBaseCard;
                    if (String.CompareOrdinal(c1.Name, c2.Name)>0)
                    {
                        temp1 = container.Children[i];
                        temp2 = container.Children[j];
                        container.Children.RemoveAt(i);
                        container.Children.RemoveAt(j - 1);
                        container.Children.Insert(i, temp2);
                        container.Children.Insert(j, temp1);
                    }
                }
            }
        }
        private bool Check(YBaseCard card)
        {
            IEnumerable<int> current=GetCurrentDeck();
            int d = 0;
            foreach (int item in current)
            {
                if (item == card.Card_TypeID)
                {
                    d++;
                    if (d == 3)
                        return false;
                }
            }
            return true;
        }
        DeckConstructLayout layout;
       
       
	}
}